package servlet.room;

import bean.pojo.Room;
import com.alibaba.fastjson2.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.format.NumberFormat;
import commons.jdbc.basic.ResponseUtil;
import commons.r.R;
import service.RoomService;
import service.impl.RoomServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * servlet.room
 * User: 坚持
 * Date: 2024/12/19 9:32
 * motto: 树欲静而风不止
 * Description:
 * Version: V1.0
 */
@WebServlet(name="RoomServlet",value = "/room/*")
public class RoomServlet extends BaseServlet {

    /**
     * 房产的添加（仅管理员）
     * @param request
     * @return
     */
    public R add(HttpServletRequest request) {
        try {
            BufferedReader bufferedReader = request.getReader();
            String req_data = bufferedReader.readLine();
            Room room = JSONObject.parseObject(req_data,Room.class);
            RoomService roomService = new RoomServiceImpl();
            Boolean add = roomService.add(room);
            return add?R.ok():R.error("添加失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("出错");
    }

    /**
     * 房产的删除  根据传入id
     * @param request
     * @return
     */
    public R deleteById(HttpServletRequest request){
        Integer roomId = NumberFormat.stringToInteger(request.getParameter("roomId"));
        RoomService roomService = new RoomServiceImpl();
        Boolean delete = roomService.deleteById(roomId);
        return delete?R.ok():R.error("删除失败");
    }

    /**
     * 房产模糊查询  管理员
     * 注：业主可查看自己的房产信息
     * @param request
     * @return
     */
    public R findBy(HttpServletRequest request){
        Integer page = NumberFormat.stringToInteger(request.getParameter("page"));
        Integer limit = NumberFormat.stringToInteger(request.getParameter("limit"));
        Integer roomId = NumberFormat.stringToInteger(request.getParameter("roomId"));
        String roomNumber = request.getParameter("roomNumber");
        String roomType = request.getParameter("roomType");
        Integer roomArea = NumberFormat.stringToInteger(request.getParameter("roomArea"));
        String remarks = request.getParameter("remarks");
        Integer userId = NumberFormat.stringToInteger(request.getParameter("userId"));
        RoomService roomService = new RoomServiceImpl();

        Long total = roomService.findByTotal( roomId, roomNumber, roomType, roomArea, remarks, userId);

        List<Room> list=null;
        if(total>0) {
            //当前页码的数据
            list = roomService.roomFindBy(page, limit, roomId, roomNumber, roomType, roomArea, remarks, userId);
            System.out.println(list.toString());
        }
        return R.ok().addData("total",total).addData("list",list);
    }

    /**
     * 房产修改  管理员
     * @param request
     * @return
     */
    public R update(HttpServletRequest request){
        try {
            BufferedReader bufferedReader = request.getReader();
            String req_data = bufferedReader.readLine();
            Room room = JSONObject.parseObject(req_data,Room.class);
            RoomService roomService = new RoomServiceImpl();
            Boolean update = roomService.update(room);
            return  update?R.ok():R.error("修改失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("出错");
    }

    public R findType(HttpServletRequest request){
        RoomService roomService = new RoomServiceImpl();
        List<Room> list = roomService.findType();
        return  R.ok().addData("list",list);
    }

    public R findRoomList(HttpServletRequest request){
        RoomService roomService = new RoomServiceImpl();
        List<Room> list = roomService.findRoomList();
        return  R.ok().addData("list",list);
    }
}
